﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon.S3;
using Amazon.S3.Model;

namespace NAntExt.Aws
{
    /// <summary>
    /// A bucket at Amazon S3.
    /// </summary>
    public class S3Bucket
    {

        /// <summary>
        /// Checks to see if a bucket with the name <c>bucketName</c> exists at Amazon S3.
        /// </summary>
        /// <param name="client">The client to connect with Amazon S3.</param>
        /// <param name="bucketName">The name of the bucket at Amazon S3.</param>
        /// <returns>Returns <c>true</c>, if the bucket exists, else <c>false</c>, if it doesn't exist.</returns>
        public static bool Exists(AmazonS3Client client, string bucketName)
        {
            if (client == null)
            {
                throw new System.ArgumentNullException("client");
            }

            try
            {
                GetBucketLocationRequest bucketExistsRequest = new GetBucketLocationRequest();
                bucketExistsRequest.WithBucketName(bucketName);
                using (GetBucketLocationResponse bucketExistsResponse = client.GetBucketLocation(bucketExistsRequest))
                {
                    return true;
                }
            }
            catch (AmazonS3Exception ex)
            {
                if (ex.Message == "The specified bucket does not exist")
                    return false;
                else
                    throw;
            }
        }

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        private S3Bucket() { }

    }
}
